package cn.kgcnews.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.kgcnews.pojo.NewsDetail;
import cn.kgcnews.pojo.PageBean;
import cn.kgcnews.service.NewsService;
import cn.kgcnews.service.impl.NewsServiceImpl;

public class SerachNews extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	this.doPost(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		int ntid = 0;
		String queryTitle = request.getParameter("queryTitle");
		String tid = request.getParameter("ntid");
		if (tid != null && !"".equals(tid)) {
			ntid = Integer.parseInt(tid);
		}
		int currentPageNo = 1; //页码
		int pageSize = 10;
		NewsService newsSerive = new NewsServiceImpl();
		//总页数
		int totalPageCount = newsSerive.queryTotalPageCount(pageSize,
				queryTitle, ntid);
		String pageNo = request.getParameter("pageno");
		if (pageNo != null && !"".equals(pageNo)) {
			currentPageNo = Integer.valueOf(pageNo);
		}
		//控制获取的页码
		if (currentPageNo == 0) {
			currentPageNo = 1;
		}
		if (currentPageNo >= totalPageCount) {
			currentPageNo = totalPageCount;
		}
		PageBean<NewsDetail> pageBean = newsSerive.queryNewsByPageNo(
				currentPageNo, pageSize, queryTitle, ntid);
		//总记录数
		int totalCount = pageBean.getTotalCount();
		pageBean.setCurrPageNo(currentPageNo);
		HttpSession session=request.getSession();
		session.setAttribute("bean", pageBean);
		session.setAttribute("queryTitle", queryTitle);
		session.setAttribute("ntid", ntid);
		response.sendRedirect("newslist.jsp");
		out.flush();
		out.close();
	}

	
	public void init() throws ServletException {
		// Put your code here
	}

}
